package com.thinkgem.dingdan.test;

import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

public class testWriteExcel {

	public static void writeExcel(){
		String excelPath = "g:/dev_desk/gen/"+new Date().getTime()+".xls";
		HSSFWorkbook workbook = null;
		try {
			workbook = new HSSFWorkbook();
		}catch(Exception e){
			System.out.println("创建Excel失败: ");
			e.printStackTrace();
		  }if(workbook!=null){
			Sheet sheet = workbook.createSheet("测试数据");
		    Row row0 = sheet.createRow(0);
					for (int i = 0; i < 6; i++) {
						BigDecimal val = new BigDecimal("123.25");
						double doubleValue = ((BigDecimal)val).doubleValue();
						Cell cell = row0.createCell(i,Cell.CELL_TYPE_NUMERIC);
						CellStyle cellStyle = workbook.createCellStyle();
						HSSFDataFormat df = workbook.createDataFormat();
						cellStyle.setDataFormat(df.getFormat("#,#0.00"));
						cell.setCellStyle(cellStyle);
						cell.setCellValue(doubleValue);
					}
					 Row row1 = sheet.createRow(1);
						for (int i = 0; i < 6; i++) {
							int val = 123;
							Cell cell = row1.createCell(i,Cell.CELL_TYPE_NUMERIC);
							CellStyle cellStyle = workbook.createCellStyle();
							cell.setCellStyle(cellStyle);
							cell.setCellValue(val);
						}
					try {
						FileOutputStream outputStream = new FileOutputStream(excelPath);
						workbook.write(outputStream);
						outputStream.flush();
						outputStream.close();
						} catch (Exception e) {
							System.out .println("写入Excel失败: ");
							e.printStackTrace();
					}
				}
	}
	
	public static void main(String[] args) {
		writeExcel();
	}

}
